//
//  TripViewController.m
//  YouGoWeGo
//
//  Created by LiuFei on 14/11/19.
//  Copyright (c) 2014年 LiuFei. All rights reserved.
//

#import "TripViewController.h"
#import "CustomTripCell.h"
#import "hotTripController.h"
#import "tripDetailViewController.h"
#import "TripModel.h"
#import "UIImageView+WebCache.h"
#import "pictureViewController.h"
#import "pictureModel.h"
#import "pictureCollectionCell.h"
#import "tirpViewCell.h"
#import "theTripModel.h"
#import "specialTopicController.h"
#import "priceViewCell.h"
#import "priceModel.h"
#import "priceWebController.h"


@interface TripViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate>

@property (retain, nonatomic, readwrite) REMenu *menu;
@property (retain, nonatomic)  NSTimer *timer;


@end
@implementation TripViewController

- (void)dealloc
{
    [_priceArray release];
    [_priceTable release];
    [_tripArray release];
    [_tripTableView release];
    [_backScroll release];
    [_collecArray release];
    [_collec release];
    [_titleButton release];
    [_tableArray release];
    [_tableView release];
    [_scroll release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
        self.tripArray = [NSMutableArray array];
        self.collecArray = [NSMutableArray array];
        self.priceArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    [self.view setBackgroundColor:[UIColor colorWithRed:249 / 255.0 green:246 / 255.0  blue:231 / 255.0  alpha:1]];
    [self.navigationItem setTitle:@"热门游记"];
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:88 / 255.0 green:224 / 255.0 blue:255 / 255.0 alpha:1]];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self createScrollView];
    [self createTableView];
    [self createConllectionView];
    [self createTripTableView];
    [self createPriceTableView];
    [self createBackScroll];
    [self createCollectionButton];
    [self createTableViewButton];
    [self createTripTableViewButton];
    [self createpriceTableViewButton];
    [self startAsync];
}



#pragma mark -
#pragma mark 创建大背景BackScrollView
- (void)createBackScroll
{
    self.backScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.backScroll addSubview:_scroll];
    [self.backScroll addSubview:_tableView];
    [self.backScroll addSubview:_collec];
    [self.backScroll addSubview:_tripTableView];
    [self.backScroll addSubview:_priceTable];
    [self.view addSubview:self.backScroll];
    [self.backScroll setContentSize:CGSizeMake(0,_priceTable.frame.size.height + _priceTable.frame.origin.y + 65)];
    [self.backScroll release];
}

#pragma mark - 
#pragma mark  创建横向滚动scrollView
- (void)createScrollView
{
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 3 * 1 - 30)];
    [_scroll setBackgroundColor:[UIColor colorWithRed:249 / 255.0 green:246 / 255.0  blue:231 / 255.0  alpha:1]];
    [_scroll setContentSize:CGSizeMake(self.view.frame.size.width * 5, 0)];
    [_scroll setShowsHorizontalScrollIndicator:YES];
    [_scroll setUserInteractionEnabled:YES];
    _scroll.delegate = self;
    [_scroll setPagingEnabled:YES];
    //[self.view addSubview:_scroll];
    [_scroll release];
    
  self.timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(scrollTimer) userInfo:nil repeats:YES];
    
}

- (void)scrollTimer
{
    CGPoint p = _scroll.contentOffset;
    [_scroll setContentOffset:CGPointMake(self.view.frame.size.width + p.x, 0)];
    if (p.x >=  self.view.frame.size.width * 4) {
        _scroll.contentOffset = CGPointMake(0, 0);
    }
    
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
     self.timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(scrollTimer) userInfo:nil repeats:YES];
    
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.timer invalidate];
    self.timer = 0;
}




#pragma mark - 
#pragma mark -  创建热门游记tableView
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,_scroll.frame.size.height + 30, self.view.frame.size.width, 208 * 5 ) style:UITableViewStylePlain];
    [_tableView setRowHeight:208];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tag = 1;
    [_tableView registerClass:[CustomTripCell class] forCellReuseIdentifier:@"ok"];
    [_tableView setContentSize:CGSizeMake(0, 0)];
    [_tableView release];
    
}


#pragma mark -
#pragma mark 创建旅行专题栏

- (void)createTripTableView
{
    self.tripTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, _collec.frame.origin.y + _collec.frame.size.height + 30, self.view.frame.size.width, 260 * 2) style:UITableViewStylePlain];
    _tripTableView.delegate = self;
    _tripTableView.dataSource = self;
    _tripTableView.tag = 2;
    [_tripTableView setRowHeight:260];
    [_tripTableView registerClass:[tirpViewCell class] forCellReuseIdentifier:@"ok1"];
    [_tripTableView release];
}

#pragma mark - 
#pragma mark 创建特价priceTableView
- (void)createPriceTableView
{
    self.priceTable = [[UITableView alloc] initWithFrame:CGRectMake(0, _tripTableView.frame.origin.y + _tripTableView.frame.size.height + 30, self.view.frame.size.width, 120 * 3) style:UITableViewStylePlain];
    [_priceTable setRowHeight:120];
    [_priceTable registerClass:[priceViewCell class] forCellReuseIdentifier:@"ok2"];
    _priceTable.tag = 3;
    _priceTable.delegate = self;
    _priceTable.dataSource = self;
    [_priceTable release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag == 1) {
        return [_tableArray count];
        
    } else if (tableView.tag == 2){
        
        return [_tripArray count];
    } else {
        
        return [_priceArray count];
    }
        
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 1) {
        CustomTripCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ok"];
        TripModel *model = [_tableArray objectAtIndex:indexPath.row];
        cell.model = model;
      [cell setBackgroundColor:[UIColor colorWithRed:249 / 255.0 green:246 / 255.0  blue:231 / 255.0  alpha:1]];
        
        return cell;
    }
    else if (tableView.tag == 2) {
        tirpViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ok1"];
        theTripModel *tripModel = [_tripArray objectAtIndex:indexPath.row];
        cell.tripModel = tripModel;
         [cell setBackgroundColor:[UIColor colorWithRed:249 / 255.0 green:246 / 255.0  blue:231 / 255.0  alpha:1]];

        return cell;
    } else {
        
        priceViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ok2"];
        priceModel *pModel = [_priceArray objectAtIndex:indexPath.row];
        cell.pModel = pModel;
        [cell setBackgroundColor:[UIColor colorWithRed:249 / 255.0 green:246 / 255.0  blue:231 / 255.0  alpha:1]];
        [cell.showImage setClipsToBounds:YES];
        return cell;
    }
}

#pragma mark -
#pragma mark 点击cell 跳转进入详情页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (tableView.tag == 1) {
        tripDetailViewController *detail = [[tripDetailViewController alloc] init];
        detail.num = indexPath.row;
        TripModel *model =  [_tableArray objectAtIndex:indexPath.row];
        detail.numid = model.id;
        [self.navigationController pushViewController:detail animated:YES];
        [detail release];
    } else if (tableView.tag == 2){
        specialTopicController *special = [[specialTopicController alloc] init];
        theTripModel *model = [_tripArray objectAtIndex:indexPath.row];
        special.web = model.url;
        [self.navigationController pushViewController:special animated:YES];
        //[special release];
    } else {
        priceWebController *priceWeb = [[priceWebController alloc] init];
        priceModel *model = [_priceArray objectAtIndex:indexPath.row];
        
        priceWeb.web = model.url;
        [self.navigationController pushViewController:priceWeb animated:YES];
        
    }
}

#pragma mark -
#pragma mark -  数据解析

- (void)startAsync
{
    NSURL *url = [NSURL URLWithString:@"http://api.breadtrip.com/featured/"];
    
    NSURLRequest *requsest = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:requsest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        NSMutableArray *arr = [dic objectForKey:@"elements"];
        NSMutableDictionary *tripDic = [arr objectAtIndex:4];
        NSMutableArray *thetripArray = [tripDic objectForKey:@"data"];
        for (NSMutableDictionary *tripdic in thetripArray) {
            theTripModel *tripModel = [[theTripModel alloc] init];
            [tripModel setValuesForKeysWithDictionary:tripdic];
            [_tripArray addObject:tripModel];
            [_tripTableView reloadData];
            [tripModel release];
        }
        
        
        NSMutableDictionary *priceDic = [arr objectAtIndex:5];
        NSMutableArray *pricArray = [priceDic objectForKey:@"data"];
        
        for (NSMutableDictionary *pricdic in pricArray) {
            priceModel *Model = [[priceModel alloc] init];
            [Model setValuesForKeysWithDictionary:pricdic];
            [_priceArray addObject:Model];
            [_priceTable reloadData];
            [Model release];
        }
        
        
        
        NSMutableDictionary *picDic = [arr objectAtIndex:2];
        NSMutableArray *picArray = [picDic objectForKey:@"data"];
        for (NSMutableDictionary *numDic in picArray) {
            pictureModel *picModel = [[pictureModel alloc] init];
            [picModel setValuesForKeysWithDictionary:numDic];
           
            [_collecArray addObject:picModel];
            [picModel release];
            [_collec reloadData];
        }
        
        
        
        NSMutableDictionary *dic0 = [arr objectAtIndex:0];
        NSMutableArray *arrdata = [dic0 objectForKey:@"data"];
        NSMutableArray *arr0 = [arrdata lastObject];
        int i = 0;
        for (NSMutableDictionary *dicpic in arr0) {
            NSString *str = [dicpic objectForKey:@"image_url"];
            
            UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * i, 0, self.view.frame.size.width, _scroll.frame.size.height)];
                //[imageview setBackgroundColor:[UIColor redColor]];
                
                [imageview setImageWithURL:[NSURL URLWithString:str]];
                [imageview setUserInteractionEnabled:YES];
                [_scroll addSubview:imageview];
                [imageview release];
            
            i ++;
        }
        
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * 4, 0, self.view.frame.size.width, _scroll.frame.size.height)];
        
        [image setImageWithURL:[NSURL URLWithString:[[arr0 firstObject] objectForKey:@"image_url"]]];
        [image setUserInteractionEnabled:YES];
        [_scroll addSubview:image];
        [image release];
        
        
        
        NSMutableDictionary *dic1 = [arr objectAtIndex:1];
        NSMutableArray *arrData = [dic1 objectForKey:@"data"];
        for (NSMutableDictionary *dic in arrData) {
            TripModel *model = [[TripModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
         
            [_tableArray addObject:model];
            [_tableView reloadData];
        }
        
        
        
        
        
        
        
        
    }];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
   
    return nil;
}

// tableView section高度为0
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}


#pragma mark -
#pragma mark 创建collectionView
- (void)createConllectionView
{
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    [flow setItemSize:CGSizeMake((self.view.frame.size.width - 30) / 2, (self.view.frame.size.width - 30) / 2)];
    NSLog(@"%f,%f",flow.itemSize.width, flow.itemSize.height);
    [flow setMinimumInteritemSpacing:10];
    [flow setMinimumLineSpacing:10];
    flow.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    
    self.collec = [[UICollectionView alloc] initWithFrame:CGRectMake(_tableView.frame.origin.x, _tableView.frame.size.height + _scroll.frame.size.height + 60, _tableView.frame.size.width, self.view.frame.size.width - 20) collectionViewLayout:flow];
    [_collec setBackgroundColor:[UIColor colorWithRed:249 / 255.0 green:246 / 255.0  blue:231 / 255.0  alpha:1]];
    [_collec registerClass:[pictureCollectionCell class] forCellWithReuseIdentifier:@"ok"];
    [_collec setContentSize:CGSizeMake(0, 0)];
    _collec.delegate = self;
    _collec.dataSource = self;
    [self.view addSubview:_collec];
    [flow release];
    [_collec release];
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [_collecArray count];
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    pictureCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ok" forIndexPath:indexPath];
    [cell setBackgroundColor:[UIColor clearColor]];
    pictureModel *picModel = [_collecArray objectAtIndex:indexPath.item];
    cell.picModel = picModel;
 
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    pictureViewController *detail = [[pictureViewController alloc] init];
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
}


#pragma mark -
#pragma mark 创建三个标题按钮


#pragma mark - 
#pragma mark 创建tableView 上面的Button

- (void)createTableViewButton
{
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, _scroll.frame.size.height, 120, 30)];
    [button setBackgroundColor:[UIColor cyanColor]];
    button.layer.cornerRadius = 10;
    //    [button.layer setBorderWidth:1];
    [button setTitle:@"热门游记" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickAction1:) forControlEvents:UIControlEventTouchUpInside];
    [self.backScroll addSubview:button];
    [button release];
}

- (void)clickAction1:(id)sender
{
    hotTripController *hotTrip = [[hotTripController alloc] init];
    
    [self.navigationController pushViewController:hotTrip animated:YES];
    [hotTrip release];
}



#pragma mark -
#pragma mark 创建collectionView 上面的Button
- (void)createCollectionButton
{
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10,_tableView.frame.size.height + _scroll.frame.size.height + 30, 120, 30)];
    [button setBackgroundColor:[UIColor cyanColor]];
    button.layer.cornerRadius = 10;
    //    [button.layer setBorderWidth:1];
    [button setTitle:@"精选图片" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickAction3:) forControlEvents:UIControlEventTouchUpInside];
    [self.backScroll addSubview:button];
    [button release];
}

- (void)clickAction3:(id)sender
{
    pictureViewController *picture = [[pictureViewController alloc] init];
    [self.navigationController pushViewController:picture animated:YES];
    [picture release];
}


#pragma mark -
#pragma mark - 创建tripTableView 上面的Button

- (void)createTripTableViewButton
{
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, _scroll.frame.size.height + _tableView.frame.size.height + _collec.frame.size.height + 60, 120, 30)];
    [button setBackgroundColor:[UIColor cyanColor]];
    button.layer.cornerRadius = 10;
    //    [button.layer setBorderWidth:1];
    [button setTitle:@"旅行专题" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickAction2:) forControlEvents:UIControlEventTouchUpInside];
    [self.backScroll addSubview:button];
    [button release];
}

- (void)clickAction2:(id)sender
{
    
}

#pragma mark -
#pragma mark 创建priceTableView 上的按钮
- (void)createpriceTableViewButton
{
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, _tripTableView.frame.size.height + _tripTableView.frame.origin.y , 120, 30)];
    [button setBackgroundColor:[UIColor cyanColor]];
    button.layer.cornerRadius = 10;
    //    [button.layer setBorderWidth:1];
    [button setTitle:@"特价旅行" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickAction4:) forControlEvents:UIControlEventTouchUpInside];
    [self.backScroll addSubview:button];
    [button release];
}

- (void)clickAction4:(id)sender
{
    
}














@end
